package action.expert;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import persistence.model.TdfkfaExpertList;
import persistence.model.TdfkfaUser;
import presentation.PageVO;
import presentation.extendmodel.TdfkfaExpertListEx;
import presentation.queryvo.ExpertListQueryVO;
import service.ExpertService;
import service.UserService;

import com.opensymphony.xwork2.ActionSupport;

import constant.ServiceConstants;

public class ManageExpertAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = -7833127304376647641L;

    private HttpServletRequest request;

    private ExpertService expertService;
    
    private UserService userService;

    private List<TdfkfaExpertListEx> expertList;

    private TdfkfaExpertList expert;

    private String[] expertId;

    private String eid;

    private String status;

    private PageVO pageVO;

    private ExpertListQueryVO queryVO;

    public String addExpert() throws Exception {
    	
    	TdfkfaUser user = new TdfkfaUser();
    	user.setUsername(expert.getUsername());
    	user.setPassword(DigestUtils.shaHex("000000"));
    	user.setCreateDate(new Date());
    	user.setEnabled(new Short("1"));
    	String ret = userService.saveExpertUser(user);
    	if ("success".equals(ret)) {
    		int ret2 = expertService.saveExpert(expert);
    		if (ret2 == ServiceConstants.SUCCESS) {
    			status = "success";
    		} else {
    			status = "failure";
    		}
    	} else if ("same username".equals(ret)){
    		status = "same username";
    	} else {
    		status = "failure"; 
    	}
        
        return SUCCESS;
    }

    public String loadExpertList() throws Exception {

        pageVO.setRecordNum(expertService.countExpert(queryVO));

        expertList = expertService.getExpertList(queryVO, pageVO.getIntPageIndex(), pageVO.getIntPageSize());

        return SUCCESS;
    }

    public String toEditExpert() throws Exception {
        if (expertId == null || expertId.length != 1) {
            return INPUT;
        } else {
            eid = expertId[0];
            request.getSession().setAttribute("eid", eid);
            return SUCCESS;
        }
    }

    public String prepareEditExpert() throws Exception {
        eid = (String) request.getSession().getAttribute("eid");
        expert = expertService.getExpert(new Integer(eid));

        if (expert == null) {
            return INPUT;
        }

        return SUCCESS;
    }

    public String submitEditExpert() throws Exception {

        int r = expertService.updateExpert(expert);
        if (r == 1) {
            status = "success";
        } else {
            status = "failure";
        }
        return SUCCESS;
    }

    public String deleteExpert() throws Exception {

        if (expertId == null || expertId.length == 0) {
            return INPUT;
        }

        Integer[] ids = new Integer[expertId.length];
        for (int i = 0; i < expertId.length; i++) {
            ids[i] = new Integer(expertId[i]);
        }

        expertService.deleteExpert(ids);

        return SUCCESS;
    }

    public void setExpertService(ExpertService expertService) {
        this.expertService = expertService;
    }

    public List<TdfkfaExpertListEx> getExpertList() {
        return expertList;
    }

    public TdfkfaExpertList getExpert() {
        return expert;
    }

    public void setExpert(TdfkfaExpertList expert) {
        this.expert = expert;
    }

    public String getEid() {
        return eid;
    }

    public void setEid(String eid) {
        this.eid = eid;
    }

    public void setExpertId(String[] expertId) {
        this.expertId = expertId;
    }

    public String getStatus() {
        return status;
    }

    public PageVO getPageVO() {
        return pageVO;
    }

    public void setPageVO(PageVO pageVO) {
        this.pageVO = pageVO;
    }

    public ExpertListQueryVO getQueryVO() {
        return queryVO;
    }

    public void setQueryVO(ExpertListQueryVO queryVO) {
        this.queryVO = queryVO;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
